<template>
  <div class="default-main ba-table-box">
    <div class="block">
      <div class="table-block">
        <el-alert class="ba-table-alert"
                  v-if="baTable.table.remark"
                  :title="baTable.table.remark"
                  type="info"
                  show-icon />
        <!-- 表格顶部菜单 -->
        <TableHeader :buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
                     :quick-search-placeholder="t('Quick search placeholder', { fields: '订单号' })">
        </TableHeader>

        <!-- <template v-if="baTable.auth('')"> -->
        <el-row style="margin: 10px 0;"
                :gutter="10">
          <el-col :span="1.5">
            <!-- v-if="baTable.auth('export')" -->
            <el-button type="warning"
                       @click="exportExcelOperate"
                       :loading="state.buttonLoading">
              <Icon color="#ffffff"
                    name="el-icon-Position" />导出
            </el-button>
          </el-col>
        </el-row>
        <!-- </template> -->
        <Table></Table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, provide } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import baTableClass from '/@/utils/baTable'
// import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { baTableApi, fileDownload } from '/@/api/common'
// import { fileUpload, exportExcel } from '/@/api/backend/warehouse/warehouseList'
import Cookies from 'js-cookie'
import { useI18n } from 'vue-i18n'
import { defaultOptButtons } from '/@/components/table'
import { useAdminInfo } from '/@/stores/adminInfo'

defineOptions({
    name: 'warehouse/warehouseList',
})
const { t } = useI18n()
const adminInfo = useAdminInfo()
const state: {
    buttonLoading: boolean
} = reactive({
    buttonLoading: false
})

const optButtons = defaultOptButtons(['edit', 'delete'])
optButtons[1].display = (row) => {
    return row.id != adminInfo.id
}

const baTable = new baTableClass(
    new baTableApi('/warehouse/warehouseList/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            {   
                label: '名称', 
                prop: 'name', 
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '请输入名称'
            },
            {
                label: t('Operate'),
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            }
        ],
        dblClickNotEditColumn: ['all']
    },
    {
        defaultItems: {
            status: '1',
        }
    }
)

provide('baTable', baTable)

baTable.mount()
baTable.getIndex()

const exportExcelOperate = () => {
    state.buttonLoading = true
    baTable.table.loading = true
    ElMessageBox.confirm(
        '是否确认导出所有数据项?',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    ).then(() => {
        // type keymaps = {
        //     [key: string]: string | number
        // }
        // const obj:keymaps = {}
        // baTable.table.filter?.search!.forEach(v => {
        //     obj[v.field] = v.val
        // }) || []
        // exportExcel('post', obj).then(response => {
        //     fileDownload(response, Cookies.get('filename') || 'file.xlsx')
        // }).catch(err => {
        //     state.buttonLoading = false
        //     baTable.table.loading = false
        // }).finally(() => { 
        //     state.buttonLoading = false
        //     baTable.table.loading = false
        // })
        console.log('exportExcelOperate')
        state.buttonLoading = false
        baTable.table.loading = false
    }).catch(() => {
        state.buttonLoading = false
        baTable.table.loading = false
    })
}

</script>

<style scoped lang="scss">
</style>
